/**
 ******************************************************************************
 * @file    lin.c
 * @author  hyseim software Team
 * @date    18-Aug-2023
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lin.h"
#include "common.h"


/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup LIN
 * @brief LIN driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LIN_Private_Functions
  * @{
  */


/**
 * @brief  Enables or disables the specified LIN peripheral.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  NewState: new state of the LIN peripheral.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LIN_Cmd(LIN_t* LINx, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LINx->CON |= LIN_CON_ENABLE;
    }
    else {
        LINx->CON &= ~LIN_CON_ENABLE;
    }
}

/**
 * @brief  Enables or disables the LIN Loopback Mode.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  NewState: new state of the Loopback Mode.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LIN_LoopBackCmd(LIN_t* LINx, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LINx->CON |= LIN_CON_LOOPEN;
    }
    else {
        LINx->CON &= ~LIN_CON_LOOPEN;
    }
}

/**
 * @brief  Enables or disables the LIN receiver block.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  NewState: new state of the receiver block.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LIN_ReceiverCmd(LIN_t* LINx, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LINx->CON |= LIN_CON_RXEN;
    }
    else {
        LINx->CON &= ~LIN_CON_RXEN;
    }
}

/**
 * @brief  Enables or disables the LIN transmitter block.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  NewState: new state of the transmitter block.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LIN_TransmitterCmd(LIN_t* LINx, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LINx->CON |= LIN_CON_TXEN;
    }
    else {
        LINx->CON &= ~LIN_CON_TXEN;
    }
}

/**
 * @brief  Config SyncBreak OneBit BclkNum.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  SyncBreakLength: This parameter can be one of the following values:
 *         @arg @ref LIN_SyncBreak_OneBitBclkNum_16
 * @return None
 */
void LIN_OneBitBclkNumConfig(LIN_t* LINx, uint32_t BclkNum)
{
    LINx->CON = (LINx->CON & ~LIN_SyncBreak_OneBitBclkNum_Mask) | BclkNum;
}

/**
 * @brief  Config SyncBreak BitNum.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  BitNum: This parameter can be one of the following values:
 *         @arg @ref LIN_SyncBreak_BitNum_11b
 *         @arg @ref LIN_SyncBreak_BitNum_12b
 *         @arg @ref LIN_SyncBreak_BitNum_13b
 *         @arg @ref LIN_SyncBreak_BitNum_14b
 * @return None
 */
void LIN_BitNumConfig(LIN_t* LINx, uint32_t BitNum)
{
    LINx->CON = (LINx->CON & ~LIN_SyncBreak_BitNum_Mask) | BitNum;
}

/**
 * @brief  Enables or disables the specified LIN interrupts.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  LIN_IT: specifies the LIN interrupt source to be enabled or disabled. 
 *         This parameter can be a combination of the following values:
 *         @arg @ref LIN_IT_TX_OVER: Transmite synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_OVER: Receive synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_ERROR: Receive synch break Error interrupt.
 * @param  NewState: new state of the specified LIN interrupt.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None.
 */
void LIN_ITConfig(LIN_t* LINx, uint32_t LIN_IT, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LINx->CON |= LIN_IT;
    }
    else {
        LINx->CON &= ~LIN_IT;
    }
}

/**
 * @brief  Checks whether the specified LIN interrupt has occurred or not.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  LIN_IT: specifies the LIN interrupt source to check.
 *         This parameter can be one of the following values:
 *         @arg @ref LIN_IT_TX_OVER: Transmite synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_OVER: Receive synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_ERROR: Receive synch break Error interrupt.
 * @return The new state of LIN_IT (SET or RESET).
 */
ITStatus_t LIN_GetITStatus(LIN_t* LINx, uint32_t LIN_IT)
{
    ITStatus_t bitstatus = RESET;

    if (LINx->INTR_STAT & LIN_IT) {
        bitstatus = SET;
    }
    else {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
 * @brief  Clears the LIN interrupt pending bits.
 * @param  LINx: Pointer to selected LIN peripheral.
 *         This parameter can be one of the following values:
 *         LIN0, LIN1, LIN2, LIN3, LIN4, LIN5, LIN6, LIN7.
 * @param  LIN_IT: specifies the interrupt pending bit to clear.
 *         This parameter can be a combination of the following values:
 *         @arg @ref LIN_IT_TX_OVER: Transmite synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_OVER: Receive synch break Over interrupt.
 *         @arg @ref LIN_IT_RX_ERROR: Receive synch break Error interrupt.
 * @return None.
 */
void LIN_ClearIT(LIN_t* LINx, uint32_t LIN_IT)
{
    LINx->INTR_STAT = LIN_IT;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
